var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var YjxBaseModel = require('./YjxBaseModel');
var CONFIG = require('../../config');


var YjxDesignUserPageModel = function (obj) {
    YjxBaseModel.call(this,obj);

    // `page_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户文档页ID',
    // `doc_id` bigint(20) unsigned NOT NULL COMMENT '用户文档ID',
    // `design_page_id` bigint(20) unsigned NOT NULL COMMENT '设计模板页ID',
    // `page_index` int(3) NOT NULL DEFAULT '0' COMMENT '页下标（0开始）',
    // `backup_flg` int(1) NOT NULL DEFAULT '0' COMMENT '0普通页; 1保留页',
    // `data` mediumtext COMMENT '模版页内容',
    // `hash_tag` varchar(100) DEFAULT '' COMMENT '编组对象标记',
    // `thumb` text COMMENT '缩略图',
    // `create_time` datetime NOT NULL,
    // `update_time` datetime NOT NULL,
    // `update_by` varchar(50) NOT NULL DEFAULT '',
    // `del_flg` int(1) NOT NULL DEFAULT '0',
    // PRIMARY KEY (`page_id`),
    // KEY `IDX_ITB_TMPL_PAGE_1` (`doc_id`,`del_flg`),
    // KEY `IDX_ITB_TMPL_PAGE_2` (`doc_id`,`del_flg`,`page_index`),
    // KEY `IDX_ITB_TMPL_PAGE_DEL_FLG` (`del_flg`)

};

YjxDesignUserPageModel.prototype = new YjxBaseModel();

// 取得表名
YjxDesignUserPageModel.prototype.getTableName = function(){
    return 'yjx_design_user_page';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
YjxDesignUserPageModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'page_id='+this.page_id;
};

/**
 * 根据doc_id/page_id逻辑删除对应pages
 *
 * @author 王俊
 * @date 2017-11-19
 *
 * @param callback 回调
 *          err: 删除是否成功
 *
 * 注意:
 * 1. 拷贝自   TgUserPageModel.prototype.delPageLogic
 * 2. 调用此方法的模型需存在以下属性(doc_id与page_id至少存在一个)
 *      doc_id : 作品唯一标识
 *      page_id: 页唯一标识
 *      update_by: 更新者
 */
YjxDesignUserPageModel.prototype.delPageLogic = function(callback){
    var THIS = this;

    pool.pool.getConnection(function(err, connection) {
        // Use the connection
        var sql = 'UPDATE yjx.yjx_design_user_page '+
            ' SET del_flg=1, update_time=sysdate(), update_by= '+THIS.update_by+
            ' WHERE del_flg = 0 ';
        var config=false;
        if(THIS.doc_id) {
            sql += ' AND doc_id= '+ THIS.doc_id;
            config=true;
        }
        if(THIS.page_id) {
            sql += ' AND page_id= '+ THIS.page_id;
            config=true;
        }
        if(!config){
            return callback("却少参数");
        }
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            callback(error);
        });
    });
};


/**
 * 根据doc_id/page_id获取page信息
 *
 * @author 王俊
 * @date 2017-11-19
 *
 * @param callback 回调
 *          err: 查询失败
 *          data: 页信息
 *
 * 注意:
 * 1. 复制自 TgUserPageModel.prototype.getPageInfoById 具体功能待测
 * 2. 调用此方法的数据模型需具备以下参数之一
 *      doc_id: 指定文档 id
 *      page_id: 指定页 id
 */
YjxDesignUserPageModel.prototype.getPageInfoById = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('YjxDesignUserPageModel.getPageInfoById Err:'+err);
        }
        // Use the connection
        var sql = ' SELECT * FROM yjx.yjx_design_user_page t WHERE t.del_flg = 0 ';
        if(THIS.doc_id) {
            sql += ' AND t.doc_id =' + THIS.doc_id;
        }
        if(THIS.page_id){
            sql += ' AND t.page_id =' + THIS.page_id;
        }
        sql+=' ORDER BY t.doc_id, t.page_index ';

        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            callback(error, result);
        });
    });
};

/**
 * 逻辑删除某文档所有页数据
 *
 * @author 王俊
 * @date 2017-11-20
 *
 * @param docId     文档作品 id
 * @param callback  回调
 *          err: 删除失败
 */
YjxDesignUserPageModel.prototype.deleteUserPageByDocId = function (docId, callback) {

    pool.pool.getConnection(function (getConErr, connection) {
       if(getConErr) {
           return callback(getConErr);
       }

       var sql =
           ' UPDATE yjx.yjx_design_user_page ydup ' +
           ' SET ydup.del_flg=1 ' +
           ' WHERE ydup.doc_id= ' + docId +
           '   AND ydup.del_flg=0 ';
       connection.query(sql, function (queryErr, result) {
           connection.release();
           if(queryErr) {
               console.log('YjxDesignUserPageModel.js [deleteUserPageByDocId] err: ' + queryErr);
           }
           return callback(queryErr);
       })
    });
};



module.exports = YjxDesignUserPageModel;

